package com.qinghuan.mvc.controller;

import com.qinghuan.mvc.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;

/**
 * @Auther: qinghuan
 * @Date: 2022/3/21 22:01
 * @Description:
 */
@Controller
public class ParamController {

    /**
     * DispatcherServlet底层，检测到HttpServletRequest，形参request表示的就是当前请求
     *
     * @param request
     * @return
     */
    @RequestMapping("/testServletAPI")
    public String testServletAPI(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username:" + username + ",password:" + password);
        return "success";
    }

    /**
     * 在控制器方法的形参位置，设置和请求参数同名的形参
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/testParam")
    public String testParam(
            @RequestParam("user_name") String username,
            String password,
            String[] hobby, @RequestHeader("Host") String host,
            @CookieValue("JSESSIONID") String JSESSION) {
        //若请求参数汇总出现多个同名的请求参数，可以在控制器方法的形参位置设置字符串类型或字符串数组来接收此参数
        //若使用字符串类型的形参，最终结果为请求参数的每一个值之间使用逗号进行拼接
        System.out.println("username:" + username + ",password:" + password + ",hobby:" + Arrays.toString(hobby));
        System.out.println("host:" + host);
        System.out.println("JSESSIONID:" + JSESSION);
        return "success";
    }

    @RequestMapping("/testBean")
    public String testBean(User user) {
        System.out.println(user);
        return "success";
    }

}
